//
//  GS_SportsViewController.m
//  GoodManNews
//
//  Created by dllo on 16/1/11.
//  Copyright © 2016年 dllo. All rights reserved.
//
#define URL(a) [NSString stringWithFormat:@"http://api.iclient.ifeng.com/ClientNews?id=DZPD,FOCUSDZPD&page=%@&gv=4.5.1&av=4.5.1&uid=352324075088508&deviceid=352324075088508&proid=ifengnews&os=android_22&df=androidphone&vt=5&screen=1080x1920&publishid=2012", a]

#import "GS_SportsViewController.h"
#import "Masonry.h"
#import "GS_SportsTableViewCell.h"
#import "GoodManNews_AFNetTool.h"
#import "GS_SportsModel.h"
#import "GS_SportsItemModel.h"
#import "GS_SportsSlideTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "GSSportsDetailsViewController.h"
#import "GS_SportsSlideViewController.h"
#import "GoodManCollectionView.h"

@interface GS_SportsViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, retain) UIView *headerV;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *itemArr;
@property (nonatomic, assign) NSInteger page;

@end

@implementation GS_SportsViewController

- (void)dealloc
{
    self.tableV.delegate = nil;
    self.tableV.dataSource = nil;
    [_tableV release];
    [_imageV release];
    [_arr release];
    [_itemArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
    self.navigationItem.title = @"fun一下";
    self.page = 1;
    [self createTableViews];
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        NSLog(@"下拉");
        [self getData];

    }];
    //     上拉加载
//    self.tableV.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
//        NSLog(@"上拉");
//        
//        [self getData];
//        //        [self.tableV reloadData];
//    }];
    self.tableV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        NSLog(@"上拉");
        
        [self getData];
                //        [self.tableV reloadData];
    }];
    
    
    // 马上进入刷新状态
    [self.tableV.mj_header beginRefreshing];

    
}

- (void)createTableViews
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.dataSource = self;
    self.tableV.delegate = self;
    [self.view addSubview:self.tableV];
    
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view);
//        make.bottom.equalTo(self.view);
        make.width.equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
        
        
    }];
//    [_imageV release];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GS_SportsItemModel *itemModel = self.arr[indexPath.row];
    if ([itemModel.type isEqualToString:@"doc"]) {
        return 100;
    } else {
        return 180;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return self.arr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    GS_SportsItemModel *itemModel = self.arr[indexPath.row];
    if ([itemModel.type isEqualToString:@"doc"]) {
        static NSString *cellStr = @"cell";
        GS_SportsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
        if (nil == cell) {
            cell = [[GS_SportsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        }
        cell.titleL.text = itemModel.title;
        cell.upL.text = itemModel.updateTime;
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:itemModel.thumbnail] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
//        cell.backgroundColor = [UIColor purpleColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        static NSString *slideCellStr = @"slideCell";
        GS_SportsSlideTableViewCell *slideCell = [tableView dequeueReusableCellWithIdentifier:slideCellStr];
        if (nil == slideCell) {
            slideCell = [[GS_SportsSlideTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:slideCellStr];
        }
        NSMutableDictionary *styleDic = [NSMutableDictionary dictionaryWithDictionary:itemModel.style];
        NSMutableArray *imageArr = [NSMutableArray array];
        for (NSString *imageStr in [styleDic objectForKey:@"images"]) {
            [imageArr addObject:imageStr];
        }
        [slideCell.firstV sd_setImageWithURL:[NSURL URLWithString:imageArr[0]] placeholderImage:nil];
        [slideCell.secondV sd_setImageWithURL:[NSURL URLWithString:imageArr[1]] placeholderImage:nil];
        [slideCell.thirdV sd_setImageWithURL:[NSURL URLWithString:imageArr[2]] placeholderImage:nil];
//        NSLog(@"%@", imageArr);
        slideCell.titleL.text = itemModel.title;
        slideCell.upL.text = itemModel.updateTime;
        slideCell.backgroundColor = [UIColor whiteColor];
        slideCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return slideCell;
    }
    
}

- (void)getData
{
    if (1 == self.page) {
        self.arr = [NSMutableArray array];
        self.itemArr = [NSMutableArray array];
    }

    NSString *page = [NSString stringWithFormat:@"%ld", self.page];
    
    [GoodManNews_AFNetTool getNetWithURL:URL(page) body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {

        for (NSMutableDictionary *dic in responseObject) {
     
            GS_SportsModel *model = [[GS_SportsModel alloc] initWithDic:dic];
            
//            NSLog(@"%@", model.type);
    
            if ([model.type isEqualToString:@"list"]) {
//                NSLog(@"%@", model.item);
                NSMutableArray *listArr = [NSMutableArray array];
                listArr = [dic objectForKey:@"item"];
                
                for (NSMutableDictionary *itemDic in listArr) {
                    GS_SportsItemModel *itemModel = [[GS_SportsItemModel alloc] initWithDic:itemDic];
                    
                    [self.arr addObject:itemModel];
                }
            } else if ([model.type isEqualToString:@"focus"]) {
                NSMutableArray *listArr = [NSMutableArray array];
                listArr = [dic objectForKey:@"item"];
                for (NSMutableDictionary *itemDic in listArr) {
                    [self.itemArr addObject:[itemDic objectForKey:@"thumbnail"]];
                }
                
            }
        }
        
        self.tableV.tableHeaderView = [GoodManCollectionView collectionViewWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200) imageArray:self.itemArr timeInterval:1.5 view:nil];
//        NSLog(@"%ld", self.arr.count);
        [self.tableV reloadData];
        [self.tableV.mj_header endRefreshing];
        [self.tableV.mj_footer endRefreshing];
        self.page += 1;
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GS_SportsItemModel *itemModel = self.arr[indexPath.row];
    if ([itemModel.type isEqualToString:@"doc"]) {
        GSSportsDetailsViewController *detailsVC = [[GSSportsDetailsViewController alloc] init];
        detailsVC.url = [itemModel.link objectForKey:@"url"];
//        NSLog(@"%@", detailsVC.url);
        [self.navigationController pushViewController:detailsVC animated:YES];
//         NSLog(@"%ld", indexPath.row);
        [detailsVC release];
    } else {
        GS_SportsSlideViewController *slideVC = [[GS_SportsSlideViewController alloc] init];
        slideVC.url = [itemModel.link objectForKey:@"url"];
        [self.navigationController pushViewController:slideVC animated:YES];
        [slideVC release];
    }
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
